package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.IsolationRoomMapper;
import com.lnking.lnkingplace.model.dto.isolation.place.PlaceDto;
import com.lnking.lnkingplace.model.entity.IsolationPlace;
import com.lnking.lnkingplace.model.entity.IsolationRoom;
import com.lnking.lnkingplace.model.request.isolation.place.PlaceAddRequest;
import com.lnking.lnkingplace.model.request.isolation.place.PlaceUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationPlaceService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-27
 */
@Service
public class IsolationPlaceServiceImpl extends CustomServiceImpl<IsolationPlaceMapper, IsolationPlace> implements IIsolationPlaceService {

    @Resource
    UserUtil userUtil;
    @Resource
    IsolationRoomMapper isolationRoomMapper;

    private IsolationRoom check(String placeId,String roomNumber){
        LambdaQueryWrapper<IsolationRoom> query = new LambdaQueryWrapper<>();
        query.eq(IsolationRoom::getPlaceId,placeId).eq(IsolationRoom::getRoomNumber,roomNumber);
        isolationRoomMapper.selectOne(query);
        return isolationRoomMapper.selectOne(query);
    }

    private String getPlaceId(String placeId){
        if(StringUtils.isBlank(placeId)){
            String name = userUtil.getUser().getName();
            return this.baseMapper.getPlaceId(name);
        }
        return placeId;
    }

    @Override
    public void add(PlaceAddRequest params) throws Exception {
        IsolationPlace check = check(params.getPlaceName());
        Assert.isTrue(null == check,"该场所已存在");

        IsolationPlace info = new IsolationPlace();
        BeanUtils.copyProperties(params,info);
        info.setUpdateTime(LocalDateTime.now());
        info.setEnableStatus("是");
        this.baseMapper.insert(info);
    }
    @Override
    public IsolationPlace check(String placeName){
        LambdaQueryWrapper<IsolationPlace> query = new LambdaQueryWrapper<>();
        query.eq(IsolationPlace::getPlaceName,placeName);
        IsolationPlace info = this.baseMapper.selectOne(query);
        return info;
    }

    @Override
    public void update(PlaceUpdateRequest params) throws Exception {
        IsolationPlace check = check(params.getPlaceName());
        Assert.isTrue(null == check ||
                check.getId().equals(params.getId()),"该场所已存在");

        IsolationPlace info = new IsolationPlace();
        BeanUtils.copyProperties(params,info);
        info.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(info);
    }

    @Override
    public PlaceDto getDetails(String id) {
        return null;
    }


}
